
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOuqualifyBo;
import com.wyzz.hr.dao.IOuqualifyDao;
import com.wyzz.hr.pojo.Ouqualify;
import com.wyzz.hr.pojo.PositionBase;


public class OuqualifyBoImpl implements IOuqualifyBo {

	public OuqualifyBoImpl() {
	}

	public IOuqualifyDao getOuqualifyDao() {
		return ouqualifyDao;
	}

	public void setOuqualifyDao(IOuqualifyDao ouqualifyDao) {
		this.ouqualifyDao = ouqualifyDao;
	}

	public List getpbQualifyByPbId(String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouqualify.class);
		dc.add(Restrictions
				.eq(Ouqualify.PROP_OUQ_PB_ID, new PositionBase(pbId)));
		dc.addOrder(Order.asc(Ouqualify.PROP_OUQ_SORT_ID));
		return ouqualifyDao.findByCriteria(dc);
	}

	public boolean addPbQualify(Ouqualify pbQualify) {
		boolean duplicateName = duplicateName(pbQualify, "add");
		if (!duplicateName) {
			pbQualify.setOuqSortId(Integer.valueOf(0));
			ouqualifyDao.saveObject(pbQualify);
			return true;
		} else {
			return false;
		}
	}

	public boolean updatePbQualify(Ouqualify pbQualify) {
		boolean duplicateName = duplicateName(pbQualify, "update");
		if (!duplicateName) {
			Ouqualify old = (Ouqualify) ouqualifyDao.loadObject(
					com.wyzz.hr.pojo.Ouqualify.class, pbQualify.getId(), null,
					new boolean[0]);
			old.setOuqName(pbQualify.getOuqName());
			old.setOuqDesc(pbQualify.getOuqDesc());
			ouqualifyDao.updateObject(old);
			return true;
		} else {
			return false;
		}
	}

	private boolean duplicateName(Ouqualify pbQualify, String operate) {
		List resultList = null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Ouqualify.class);
		dc.add(Restrictions
				.eq(Ouqualify.PROP_OUQ_PB_ID, pbQualify.getOuqPbId()));
		dc
				.add(Restrictions.eq(Ouqualify.PROP_OUQ_NAME, pbQualify
						.getOuqName()));
		if (operate.equals("update"))
			dc.add(Restrictions.not(Restrictions.eq(Ouqualify.PROP_ID,
					pbQualify.getId().trim())));
		resultList = ouqualifyDao.findByCriteria(dc);
		return resultList != null && resultList.size() != 0;
	}

	public boolean delPbQualify(String id) {
		try {
			ouqualifyDao.deleteObject(com.wyzz.hr.pojo.Ouqualify.class, id);
			return true;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void savePbQualifyOrder(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			ouqualifyDao.exeHql((new StringBuilder()).append(
					"update Ouqualify set ouqSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	private IOuqualifyDao ouqualifyDao;
}
